package com.yowasa.xiuxian.service.helper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yowasa.xiuxian.entity.enums.RefreshType;
import com.yowasa.xiuxian.entity.po.XiuxianResourcePO;
import com.yowasa.xiuxian.mapper.XiuxianResourceMapper;
import com.yowasa.xiuxian.util.DateXUtil;
import com.yowasa.xiuxian.util.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ResourceHelper {
    @Autowired
    private XiuxianResourceMapper xiuxianResourceMapper;


    /**
     * 检查是否具有资源
     *
     * @param guildId
     * @param resourceTag
     * @param refreshType
     * @return
     */
    public boolean checkHaveResource(String guildId, String resourceTag, RefreshType refreshType) {
        XiuxianResourcePO old = xiuxianResourceMapper.selectOne(new LambdaQueryWrapper<XiuxianResourcePO>()
                .eq(XiuxianResourcePO::getGuildId, guildId)
                .eq(XiuxianResourcePO::getRefreshType, refreshType.getId())
                .eq(XiuxianResourcePO::getResourceTag, resourceTag)
        );
        switch (refreshType) {
            case JIU_XING:
                if (old == null) {
                    XiuxianResourcePO po = new XiuxianResourcePO();
                    po.setGuildId(guildId);
                    po.setResourceValue(1);
                    po.setResourceTag(resourceTag);
                    po.setRefreshType(refreshType.getId());
                    xiuxianResourceMapper.insert(po);
                    return true;
                } else {
                    return old.getResourceValue() > 0;
                }

            case LOCK_TIME:
                if (old == null) {
                    XiuxianResourcePO po = new XiuxianResourcePO();
                    po.setGuildId(guildId);
                    po.setResourceValue(1);
                    po.setResourceTag(resourceTag);
                    po.setRefreshType(refreshType.getId());
                    xiuxianResourceMapper.insert(po);
                    return true;
                } else {
                    return new Date().after(old.getLockEndTime());
                }
            case EVENT_LOCK:
            case ENTITY:
                if (old == null) {
                    return false;
                } else {
                    return old.getResourceValue() > 0;
                }
            default:
                return false;
        }
    }

    /**
     * 使用资源
     *
     * @param guildId
     * @param resourceTag
     */
    public void useResource(String guildId, String resourceTag) {
        XiuxianResourcePO old = xiuxianResourceMapper.selectOne(new LambdaQueryWrapper<XiuxianResourcePO>()
                .eq(XiuxianResourcePO::getGuildId, guildId)
                .eq(XiuxianResourcePO::getResourceTag, resourceTag)
        );
        old.setResourceValue(old.getResourceValue() - 1);
        xiuxianResourceMapper.updateById(old);
    }

    /**
     * 锁定资源
     *
     * @param guildId     频道id
     * @param resourceTag 资源标识
     * @param days        锁定时间
     */
    public void lockResource(String guildId, String resourceTag, int days) {
        XiuxianResourcePO old = xiuxianResourceMapper.selectOne(new LambdaQueryWrapper<XiuxianResourcePO>()
                .eq(XiuxianResourcePO::getGuildId, guildId)
                .eq(XiuxianResourcePO::getRefreshType, RefreshType.LOCK_TIME.getId())
                .eq(XiuxianResourcePO::getResourceTag, resourceTag)
        );
        if (old == null) {
            XiuxianResourcePO po = new XiuxianResourcePO();
            po.setGuildId(guildId);
            po.setResourceValue(1);
            po.setResourceTag(resourceTag);
            po.setRefreshType(RefreshType.LOCK_TIME.getId());
            po.setLockEndTime(DateXUtil.getTo(days));
            xiuxianResourceMapper.insert(po);
        } else {
            old.setLockEndTime(DateXUtil.getTo(days));
            xiuxianResourceMapper.updateById(old);
        }
    }


    public void addResource(String guildId, String resourceTag) {
        RefreshType type = RefreshType.EVENT_LOCK;
        XiuxianResourcePO old = xiuxianResourceMapper.selectOne(new LambdaQueryWrapper<XiuxianResourcePO>()
                .eq(XiuxianResourcePO::getGuildId, guildId)
                .eq(XiuxianResourcePO::getRefreshType, type.getId())
                .eq(XiuxianResourcePO::getResourceTag, resourceTag)
        );
        if (old == null) {
            XiuxianResourcePO po = new XiuxianResourcePO();
            po.setGuildId(guildId);
            po.setResourceValue(1);
            po.setResourceTag(resourceTag);
            po.setRefreshType(type.getId());
            xiuxianResourceMapper.insert(po);
        } else {
            old.setResourceValue(old.getResourceValue() + 1);
            xiuxianResourceMapper.updateById(old);
        }
    }

    /**
     * 刷新实体
     * @param guildId
     * @param resourceTag
     * @param entity
     * @param <T>
     */
    public <T> void refreshEntity(String guildId, String resourceTag,T entity) {
        RefreshType type = RefreshType.ENTITY;
        XiuxianResourcePO old = xiuxianResourceMapper.selectOne(new LambdaQueryWrapper<XiuxianResourcePO>()
                .eq(XiuxianResourcePO::getGuildId, guildId)
                .eq(XiuxianResourcePO::getRefreshType, type.getId())
                .eq(XiuxianResourcePO::getResourceTag, resourceTag)
        );
        String jsonStr = JSONUtils.toJson(entity);
        if (old == null) {
            XiuxianResourcePO po = new XiuxianResourcePO();
            po.setGuildId(guildId);
            po.setResourceValue(1);
            po.setResourceTag(resourceTag);
            po.setRefreshType(type.getId());
            po.setResourceAttr(jsonStr);
            xiuxianResourceMapper.insert(po);
        } else {
            old.setResourceValue(1);
            old.setResourceValue(old.getResourceValue());
            old.setResourceAttr(jsonStr);
            xiuxianResourceMapper.updateById(old);
        }
    }

    public <T> T getEntity(String guildId, String resourceTag,Class<T> clazz) {
        RefreshType type = RefreshType.ENTITY;
        XiuxianResourcePO old = xiuxianResourceMapper.selectOne(new LambdaQueryWrapper<XiuxianResourcePO>()
                .eq(XiuxianResourcePO::getGuildId, guildId)
                .eq(XiuxianResourcePO::getRefreshType, type.getId())
                .eq(XiuxianResourcePO::getResourceTag, resourceTag)
        );
        if (old == null) {
            return null;
        } else {
            if (old.getResourceValue() <= 0) {
                return null;
            }
            if (StringUtils.isEmpty(old.getResourceAttr())) {
                return null;
            }
            return JSONUtils.toObject(old.getResourceAttr(),clazz);
        }
    }


    //刷新九星连珠的资源
    public void refreshJiuXing() {
        xiuxianResourceMapper.update(null, new LambdaUpdateWrapper<XiuxianResourcePO>()
                .set(XiuxianResourcePO::getResourceValue, 1)
                .eq(XiuxianResourcePO::getRefreshType, RefreshType.JIU_XING.getId())
        );
    }


}
